$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'grass/list',
        datatype: "json",
        colModel: [			
			{ label: 'grassId', name: 'grassId', index: 'grass_id', width: 50, key: true },
			{ label: '所属户主', name: 'userName', index: 'user_name', width: 80 }, 			
			{ label: '户主身份证号', name: 'userId', index: 'user_id', width: 80 }, 			
			{ label: '草场位置', name: 'grassLocal', index: 'grass_local', width: 80 }, 			
			{ label: '草场界线', name: 'grassLimit', index: 'grass_limit', width: 80 }, 			
			{ label: '草场面积', name: 'grassArea', index: 'grass_area', width: 80 }, 			
			{ label: '记录时间', name: 'time', index: 'time', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		user_name:'',
        grass_local:'',
		grass: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		namequery: function (event) {
                			//input.focus();
                			//alert(vm.starttime);
                			 $("#jqGrid").jqGrid('setGridParam',{
                				 url:baseURL + 'grass/namelist',
                				 postData:{
                					 'user_name':vm.user_name,
                					 'grass_local':vm.grass_local
                				 },
                				 datattype:'json',
                				 mtyp:'GET',
                				 viewrecords: true,
                			     height: 385,
                			     rowNum: 10,
                			     rowList : [10,30,50],
                			     rownumbers: true,
                			     rownumWidth: 25,
                			     autowidth:true,
                			     multiselect: true,
                			     pager: "#jqGridPager",
                			     jsonReader : {
                			         root: "page.list",
                			         page: "page.currPage",
                			         total: "page.totalPage",
                			         records: "page.totalCount"
                			     },
                			     prmNames : {
                			         page:"page",
                			         rows:"limit",
                			         order: "order"
                			     },
                			 }).trigger('reloadGrid');


        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.grass = {};
		},
		update: function (event) {
			var grassId = getSelectedRow();
			if(grassId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(grassId)
		},
		saveOrUpdate: function (event) {
			var url = vm.grass.grassId == null ? "grass/save" : "grass/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.grass),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var grassIds = getSelectedRows();
			if(grassIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "grass/delete",
                    contentType: "application/json",
				    data: JSON.stringify(grassIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(grassId){
			$.get(baseURL + "grass/info/"+grassId, function(r){
                vm.grass = r.grass;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});